//
//  WCG_N_ProviderDetailViewController.m
//  Project_B
//
//  Created by apple on 15/11/26.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "WCG_N_ProviderDetailViewController.h" // 第二页表视图

#import "GXProviderDetailTableViewCell.h"      // 第二页cell
#import "WCG_N_ProviderDetailHeaderView.h"     // 头部视图
#import "GXProviderDetailModel.h"              // 细节Model
#import "UIImageView+WebCache.h"               // 网络图片
#import "GXiaNetHandle.h"                      // 数据解析
#import "WebsIteManager.h"                     // 网址管理器
#import "GXProviderDataModel.h"                // 第一页数据模型
#import "WCG_N_ProviderDetailItemViewController.h" // DetailItem 页面


@interface WCG_N_ProviderDetailViewController () <UITableViewDataSource, UITableViewDelegate>

// 数据数组
@property (nonatomic, retain) NSMutableArray *providerDetailArr;
//造物者信息
@property (nonatomic, retain) NSDictionary *userDic; //
@property (nonatomic, assign) NSInteger page; //
@end

@implementation WCG_N_ProviderDetailViewController

#pragma mark >>> 视图将要出现
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
    // 王霄写
    UIView *temp = self.tabBarController.view.subviews[2];
    [UIView animateWithDuration:1 animations:^{
        temp.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 49);
    }];
    
}

#pragma mark >>> 视图已经出现
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    /* 数据请求 */
    ////////////////////
    [self getProviderDetailData];
    ////////////////////
}

#pragma mark >>> 重写按钮响应方法
- (void)bottomBtnAction:(UIButton *)btn {
    // 返回
    if (btn == self.backBtn) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

#pragma mark >>> 数据请求
- (void)getProviderDetailData {
    NSString *url = [NSString stringWithFormat:kProviderDetailWeb, self.provider_id, 1L];
    [GXiaNetHandle getUrl:url cookie:kProviderDetailCookie success:^(id result) {
        self.providerDetailArr = [GXProviderDetailModel transformWithArray:result[@"items"]];
        self.page = [result[@"page"] integerValue];
        
        /* 创建表视图 */
        [self createTableViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        
        //注册
        [self.tableView registerClass:[GXProviderDetailTableViewCell class] forCellReuseIdentifier:@"h"];
        
        /* 显示头部视图 */

        [self createHeaderView];

        /* 显示底部返回按钮 */
        [self createButtomBackButtonIsHiddenShareBtn:YES];
        [self.backBtn addTarget:self action:@selector(bottomBtnAction:) forControlEvents:UIControlEventTouchUpInside];
     
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}
#pragma mark >>> 创建头部视图

- (void)createHeaderView {
    //创建头部视图 添加到父控件 设置背景颜色
    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    WCG_N_ProviderDetailHeaderView *headerView = [[WCG_N_ProviderDetailHeaderView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, 100)];
    [tempView addSubview:headerView];
    [tempView setBackgroundColor:[UIColor colorWithRed:40.f / 255 green:43.f /255 blue:51.f / 255  alpha:1]];
    [headerView setBackgroundColor:[UIColor clearColor]];
    self.tableView.tableHeaderView = tempView;
    
    CGRect descLabelFrame = headerView.providerDescLabel.frame;
    descLabelFrame.size.height = 50;
    headerView.providerDescLabel.frame = descLabelFrame;
    
    //取到造物者详情模型 改写简介标签的高度 给控件赋值
    GXProviderDetailModel *m = [self.providerDetailArr lastObject];
    headerView.providerDetailModel = m;
}

#pragma mark >>> tableView的代理/ 数据源
/* 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.providerDetailArr.count) {
        return self.providerDetailArr.count;
    } else {
        
        return 0;
    }
}

//每行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GXProviderDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"h"];
    
    if (self.providerDetailArr.count > indexPath.row) {
        cell.detailModel = self.providerDetailArr[indexPath.row];
        return cell;
    } else {
        return nil;
        
    }
}

// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 127;
}

/*
 >> `` Description 造物者详情页跳转至造物者响应条目页``
 >>  <1> 跳转时隐藏tabBar
 >>  <2> 传递网址到条目页
 */

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 选后消失痕迹
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // item 页面
    WCG_N_ProviderDetailItemViewController *itemVC = [[WCG_N_ProviderDetailItemViewController alloc] init];

    [self.tabBarController setHidesBottomBarWhenPushed:YES];
    itemVC.providerModel = self.providerDetailArr[indexPath.row];
    [self.navigationController pushViewController:itemVC animated:YES];
    
}



















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
